package org.west.sky.frame.activiti.study.chapter19;

import org.activiti.engine.FormService;
import org.activiti.engine.RepositoryService;
import org.activiti.engine.TaskService;
import org.activiti.engine.form.FormProperty;
import org.activiti.engine.form.StartFormData;
import org.activiti.engine.form.TaskFormData;
import org.activiti.engine.repository.Deployment;
import org.activiti.engine.repository.ProcessDefinition;
import org.activiti.engine.runtime.ProcessInstance;
import org.activiti.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.west.sky.frame.activiti.study.chapter19.demo0301.InnerApplyForm;
import org.west.sky.frame.activiti.study.chapter19.demo0301.OuterApplyForm;

import java.io.InputStream;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * author: chz
 * date: 2025/10/11
 * description: 表单管理-外部表单
 */
@Controller
@RequestMapping("/chapter19/outer")
public class RunDemo19_4_1 {

    @Autowired
    private FormService formService;

    @Autowired
    private TaskService taskService;

    @Autowired
    private RepositoryService repositoryService;

    /**
     * 外部表单-获取开始节点表单内容
     *
     * @return
     */
    @RequestMapping(value = "/getStartForm")
    public String getTaskForm(Model model) {

        ProcessDefinition processDefinition = repositoryService.createProcessDefinitionQuery().processDefinitionKey("Chapter19_4_1").latestVersion().singleResult();
        if (null == processDefinition) {
            InputStream startFormStream = getClass().getClassLoader().getResourceAsStream("processes/chapter19/start.form");
            InputStream taskFormStream = getClass().getClassLoader().getResourceAsStream("processes/chapter19/task.form");
            Deployment deploy = repositoryService.createDeployment()
                    .addInputStream("start.form", startFormStream)
                    .addInputStream("task.form", taskFormStream)
                    .addClasspathResource("processes/chapter19/Chapter19_4_1.bpmn20.xml").deploy();
            processDefinition = repositoryService.createProcessDefinitionQuery().deploymentId(deploy.getId()).singleResult();
        }

        Object renderedStartForm = formService.getRenderedStartForm(processDefinition.getId());

        model.addAttribute("renderedStartForm", renderedStartForm);
        model.addAttribute("processDefinitionId", processDefinition.getId());

        return "chapter19/demo41";
    }

    /**
     * 外部表单-填写表单，发起流程
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/startProcess", method = RequestMethod.POST)
    public String start(@RequestParam("processDefinitionId") String processDefinitionId, @ModelAttribute OuterApplyForm model) {
        System.out.println("processDefinitionId:" + processDefinitionId);
        System.out.println(model);

        StartFormData formData = formService.getStartFormData(processDefinitionId);

        List<FormProperty> formProperties = formData.getFormProperties();

        Map<String, String> propertiesMap = new HashMap<>();
        propertiesMap.put("studentName", model.getStudentName());
        propertiesMap.put("sex", model.getSex());
        propertiesMap.put("birthDate", model.getBirthDate());
        propertiesMap.put("stature", model.getStature().toString());
        propertiesMap.put("reason", model.getReason());

        ProcessInstance processInstance = formService.submitStartFormData(processDefinitionId, propertiesMap);

        Task task = taskService.createTaskQuery().processInstanceId(processInstance.getId()).singleResult();
        return "流程发起成功，下一任务id:" + task.getId();
    }


    /**
     * 外部表单 获取任务表单
     *
     * @return
     */
    @RequestMapping(value = "/getTaskForm", method = RequestMethod.GET)
    public String getTask(@RequestParam("taskId") String taskId, Model model) {
        System.out.println("taskId:" + taskId);

        Object renderedTaskForm = formService.getRenderedTaskForm(taskId);

        model.addAttribute("renderedTaskForm", renderedTaskForm);
        model.addAttribute("taskId", taskId);
        return "chapter19/demo42";
    }

    /**
     * 外部表单-填写表单，完成任务
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/completeTask", method = RequestMethod.POST)
    public String completeTask(@RequestParam("taskId") String taskId, @ModelAttribute InnerApplyForm form) {
        System.out.println("taskId:" + taskId);
        System.out.println(form);

        TaskFormData formData = formService.getTaskFormData(taskId);

        List<FormProperty> formProperties = formData.getFormProperties();

        Map<String, String> propertiesMap = new HashMap<>();
        propertiesMap.put("name", form.getName());
//        propertiesMap.put("sex", form.getSex());
//        propertiesMap.put("birthDate", form.getBirthDate());
//        propertiesMap.put("height", form.getHeight().toString());
//        propertiesMap.put("reason", form.getReason());
        propertiesMap.put("result", form.getResult());
        propertiesMap.put("audit", form.getAudit());

        formService.submitTaskFormData(taskId, propertiesMap);
        return "完成任务";
    }
}
